package com.k7.mobile.platform.web.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;

import com.k7.mobile.platform.bo.IUserBO;
import com.k7.mobile.platform.dataobject.UserDO;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.qike.java.pojo.User;

public class AdminInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 6301129967419590955L;

	private IUserBO userBO;
	public void setUserBO(IUserBO userBO) {
		this.userBO = userBO;
	}
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = invocation.getInvocationContext();   
		HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);   
		User u = userBO.isLogin(request);
		if (u != null) {// 已登录,则判断是否是管理员
			UserDO user = userBO.getUserById(u.getUid().longValue());
			if (user.getAdminRole().intValue() != 1) {// 不是管理员。重新去登录
				return Action.LOGIN;
			}else{
				return invocation.invoke();
			}
		}else{//未登录
			return Action.LOGIN;
		}
	}
}
